package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.widgets.banner.holder.Holder;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.BannerImg;

import static android.R.attr.data;

/**
 * Created by 刘涛 on 2017/8/18.
 */

public class IndexAdHolder implements Holder<BannerImg> {

    private int width;
    private ImageView imageView;
    private FrameLayout frameLayout;
    private FrameLayout layout;
    private View.OnClickListener onClickListener;

    @Override
    public View createView(Context context) {
        Context mContext = context;
        width = DeviceUtils.getDisplay(mContext).widthPixels;
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_banner_img, null, false);
        imageView = (ImageView) view.findViewById(R.id.img);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        layout = (FrameLayout) view.findViewById(R.id.layout);
        frameLayout = (FrameLayout) view.findViewById(R.id.bg);
        return view;
    }

    @Override
    public void UpdateUI(Context context, int position, BannerImg data) {
        Glide.with(context)
                .load(data.getImage())
                .placeholder(R.mipmap.ic_default)
                .error(R.mipmap.ic_default)
                .into(imageView);
        //imageView.setTag(data);
        //imageView.setOnClickListener(onClickListener);
        layout.setTag(data);
        layout.setOnClickListener(onClickListener);
        if (!TextUtils.isEmpty(data.getUrl())) {
            frameLayout.setVisibility(View.VISIBLE);
        } else {
            frameLayout.setVisibility(View.GONE);
        }
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
